import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'package:shop/config/config.dart';
import 'package:shop/http/HttpUtil.dart';
import 'package:shop/route/my_route.dart';
import 'package:shop/shop/merchant/GoodsInfoEdit.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SoldOutPage extends StatefulWidget {
  int type;//类型  0：售罄商品   1：下架商品
  SoldOutPage({Key key, this.title, this.type}) : super(key: key);

  final String title;

  @override
  _SoldOutPage createState() => _SoldOutPage(type);
}

class _SoldOutPage extends State<SoldOutPage> {

  int type=0;//类型
  String merchantID=""; //商户ID
  List goodsList=[]; //商品集合
  SharedPreferences prefs;
  //初始化数据
  _SoldOutPage(type){
    this.type=type;
    _getGoodsList();
  }

  // 加载商户的商品分类
  _getGoodsList() async{
    prefs = await SharedPreferences.getInstance();
    merchantID = prefs.getString("merchantID");
    var res  =await HttpUtil().get("/waimai/mGoods/getSoldOutPage?mId="+merchantID+"&type="+type.toString());
    if(res['code']==200){
      goodsList = res['data'];
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context , width: 464 , height: 820) ;
    return Scaffold(
      body: SingleChildScrollView(
        child: Container(
          padding: EdgeInsets.fromLTRB(0, 15, 0, 0),
          child: Column(
            children: [
              _getGoods(),
            ],
          ),
        ),
      ),
    );
  }

  Widget _getGoods(){
    List<Widget> x = [];
    for(int i=0;i<goodsList.length;i++){
      x.add(
        GestureDetector(
          child:  Container(
            padding: EdgeInsets.symmetric(horizontal: 10),
            child: Column(
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Container(
                      child: Row(
                        children: [
                          // Image.asset("assets/7.png",width: 60,height: 60,),
                          Image.network(Config.getInstance().getHost()+goodsList[i]['gimages'],width: 60,height: 60,),
                          SizedBox(width: 20,),
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(goodsList[i]['gname']),
                              Text("月售:"+goodsList[i]['gsellnum'].toString()),
                              Text("￥"+goodsList[i]['gprice'].toString()),
                            ],
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    Container(
                      padding: EdgeInsets.symmetric(vertical: 5,horizontal: 10),
                      decoration: BoxDecoration(
                        border: Border.all(color: Colors.black , width: 0) ,
                      ),
                      child: type==1 ? GestureDetector(child: Text("上架"),onTap: () async {
                        var x = await HttpUtil().get("/waimai/mGoods/goodsUp?gid="+goodsList[i]['id']);
                        if(x['code']==200){
                          Fluttertoast.showToast(msg: "上架成功");
                          setState(() {

                          });
                        }
                      },) : Text(""),
                    ),
                    SizedBox(width: 20,),
                    GestureDetector(
                      child: Container(
                        padding: EdgeInsets.symmetric(vertical: 5,horizontal: 20),
                        // color: Colors.orangeAccent,
                        decoration: BoxDecoration(
                          color: Colors.orangeAccent,
                          border: Border.all(color: Colors.black , width: 0) ,
                        ),
                        child: Text("编辑",style: TextStyle(color: Colors.white),),
                      ),
                      onTap: (){
                        // print("修改商品："+goodsList[i]['gname']);
                        var tu6;var tu7;var tu8;
                        if(goodsList[i]['image6'] != null){
                          tu6 = goodsList[i]['image6'].toString().substring(2,goodsList[i]['image6'].toString().length-1);
                        }
                        if(goodsList[i]['image7'] != null){
                          tu7 = goodsList[i]['image7'].toString().substring(2,goodsList[i]['image7'].toString().length-1);
                        }
                        if(goodsList[i]['image8'] != null){
                          tu8 = goodsList[i]['image8'].toString().substring(2,goodsList[i]['image8'].toString().length-1);
                        }
                        List guigess = json.decode(goodsList[i]['guige']);
                        MyRoute.push(GoodsInfoEdit(type: 2,guiges: guigess,tu1: goodsList[i]['gimages'],tu2: goodsList[i]['image2'],tu3: goodsList[i]['image3'],tu4: goodsList[i]['image4'],tu5: goodsList[i]['image5'],tu6: tu6,tu7: tu7,tu8: tu8,gName: goodsList[i]['gname'],gPrice: goodsList[i]['gprice'].toString(),goodsTypeName: goodsList[i]['mgoodsType']['name'],danwei: goodsList[i]['gdanwei'],kucun: goodsList[i]['kucun'].toString(),miaoshu: goodsList[i]['jianjie'],id: goodsList[i]['id'],));
                      },
                    )
                  ],
                ),
                SizedBox(height: 5,),
                Divider()
              ],
            ),
          ),
          onTap: (){

          },
          behavior: HitTestBehavior.opaque,
        ),
      );
    }
    return Column(
      children: x,
    );
  }
}